<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<form class="layui-form" lay-filter="formData" id="formData">
    <div class="layui-btn-container" style="position: absolute;bottom: 0;left:43%">
        <button type="button" class="layui-btn" lay-submit="" lay-filter="addCondition" id="addCondition">添加</button>
        <a class="layui-btn layui-btn-danger" id="cancel">取消</a>
    </div>
    <div class="filterItem">
        <div class="layui-form-item" style="border-top:1px solid #ccc;margin-bottom:0;border-bottom:1px solid #ccc;">
            <div class="layui-inline">
                <label class="layui-form-label">过滤名称: </label>
                <div class="layui-input-block">
                    <input type="text" name="filterName" id="filterName"  lay-verify="required" class="layui-input" placeholder="过滤名称" maxlength="7">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">过滤Key: </label>
                <div class="layui-input-block">
                    <input type="text" name="filterKey" id="filterKey" lay-verify="required" class="layui-input" placeholder="过滤Key" maxlength="7">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-bottom:0;border-bottom:1px solid #ccc;">
            <div class="layui-inline" style="width: 429px;">
                <label class="layui-form-label">脚本类型:</label>
                <div class="layui-input-block block-select-root" style="width: 75%">
                    <div class="fieldOpt">
                        <select name="scriptType" lay-filter="scriptType" id="scriptType">
                            <option value="1">条件脚本</option>
                            <option value="2">SQL</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <fieldset class="layui-elem-field" id="conditionSetting" style=" margin:0 15px;border:1px solid #ccc;">
        <legend>条件设置</legend>
        <div class="layui-field-box">
            <div class="table-top-right"  style=" margin-bottom: 30px;">
                <div class="toolBar" style="margin:0;width: 212px;position: absolute;right: 0;">
                    <a class="layui-btn layui-btn-xs" id="addTerm">添加条件</a>
                    <a class="layui-btn layui-btn-xs" id="addScript">添加脚本</a>
                    <!--<a class="layui-btn layui-btn-xs">组合规则</a>-->
                    <!--<a class="layui-btn layui-btn-xs">拆分规则</a>-->
                    <a class="layui-btn layui-btn-xs layui-btn-danger" id="deleteCondition">删除</a>
                </div>
            </div>
            <div id="conditionalScript" style="border:2px solid #ccc;margin:5px 0 0 0;height: 255px;overflow-y: auto"></div>

            <!--隐藏的部分 拼接字符串用-->
            <div id="judge-condition" style="display: none">
                <!-- 1.数字的判断 -->
                <span  id="judgeCon-1" class="judge-condition" >
                    <select  name="judgeCondition" lay-ignore="">
                        <option value="1">等于</option>
                        <option value="2">不等于</option>
                        <option value="3">大于</option>
                        <option value="4">大于等于</option>
                        <option value="5">小于</option>
                        <option value="6">小于等于</option>
                    </select>
                    <input type="text" style="width: 55px;vertical-align: text-bottom;">
                    <select  name="judgeCondition" lay-ignore="">
                        <option value="1">等于</option>
                        <option value="2">不等于</option>
                        <option value="3">大于</option>
                        <option value="4">大于等于</option>
                        <option value="5">小于</option>
                        <option value="6">小于等于</option>
                    </select>
                    <input type="text" style="width: 55px;vertical-align: text-bottom;">
                </span>
                <!-- 2.字符串的判断 -->
                <span  id="judgeCon-2"  class="judge-condition">
                    <select name="judgeCondition" lay-ignore="">
                        <option value="1">等于</option>
                        <option value="3">等于(忽略大小写)</option>
                        <option value="2">不等于</option>
                        <option value="4">like</option>
                        <option value="5">like左</option>
                        <option value="6">like右</option>
                    </select>
                    <input type="text" style="vertical-align: text-bottom;">
                </span>
                <!--3.日期的判断 -->
                <span  id="judgeCon-3" class="judge-condition" >
                    <select  name="judgeCondition" lay-ignore="">
                        <option value="1">等于</option>
                        <option value="2">不等于</option>
                        <option value="3">大于</option>
                        <option value="4">大于等于</option>
                        <option value="5">小于</option>
                        <option value="6">小于等于</option>
                    </select>
                    <input type="text" style="width: 55px;vertical-align: text-bottom;" class="renderDate">
                    <select  name="judgeCondition" lay-ignore="">
                        <option value="1">等于</option>
                        <option value="2">不等于</option>
                        <option value="3">大于</option>
                        <option value="4">大于等于</option>
                        <option value="5">小于</option>
                        <option value="6">小于等于</option>
                    </select>
                    <input type="text" style="width: 55px;vertical-align: text-bottom;" class="renderDate">
                </span>
                <!--4.字典的判断-->
                <span  id="judgeCon-4"  class="judge-condition" style="float: left">
                    <select name="judgeCondition" lay-ignore="">
                        <option value="1">等于</option>
                        <option value="2">不等于</option>
                    </select>
                </span>
                <!--5.选择器的判断-->
                <span  id="judgeCon-5" class="judge-condition">
                    <select  name="judgeCondition" lay-ignore="" onchange="judgeConditionChange(this)">
                        <option value="1">包含</option>
                        <option value="2">不包含</option>
                        <option value="3">等于变量</option>
                        <option value="4">不等于变量</option>
                    </select>
                </span>
                <!--6.变量 CommonVar-->
                <span  id="judgeCon-6" class="judge-condition">
                    <select  name="judgeCondition" lay-ignore=""></select>
                </span>
                <!--7.人员选择器-->
                <span  id="judgeCon-7" class="judge-condition">
                    <input type="text" readonly style="vertical-align: text-bottom">
                    <button type="button" onclick="selectUser(this)" style="vertical-align: text-bottom" class="layui-btn layui-btn-xs selectUser">选择</button>
                    <input type="hidden">
                </span>
                <!--8.组织选择器-->
                <span  id="judgeCon-8" class="judge-condition">
                    <input type="text" readonly style="vertical-align: text-bottom">
                    <button type="button" onclick="selectOrg(this)" style="vertical-align: text-bottom" class="layui-btn layui-btn-xs selectUser">选择</button>
                    <input type="hidden">
                </span>
                <!--9.角色选择器-->
                <span  id="judgeCon-9" class="judge-condition">
                    <input type="text" readonly style="vertical-align: text-bottom">
                    <button type="button" onclick="selectRole(this)" style="vertical-align: text-bottom" class="layui-btn layui-btn-xs selectUser">选择</button>
                    <input type="hidden">
                </span>
                <!--10.下拉选项-->
                <span  id="judgeCon-10" class="judge-condition">
				    <select name="judgeCondition" lay-ignore="" style="vertical-align: text-bottom"></select>
			    </span>
                <!--11.复选按钮-->
                <span  id="judgeCon-11" class="judge-condition">

                </span>
                <!--12.单选按钮-->
                <span  id="judgeCon-12" class="judge-condition">

                </span>
                <!--13.岗位选择器-->
                <span  id="judgeCon-13" class="judge-condition">
                    <input type="text" readonly style="vertical-align: text-bottom">
                    <button type="button" style="vertical-align: text-bottom" class="layui-btn layui-btn-xs selectUser">选择</button>
                    <input type="hidden">
                </span>
            </div>
        </div>
    </fieldset>
    <fieldset class="layui-elem-field" style="display:none; margin:0 15px;border:1px solid #ccc;" id="sqlSet">
        <legend>SQL设置</legend>
        <div class="layui-field-box">
            <table class="table-detail">
                <tr>
                    <th style="position: relative;"><i class="layui-icon layui-icon-help" style="color:blue;"></i>
                        <div class="help">
                            <p>该脚本为groovyScript脚本,返回值为可执行的sql语句</p>
                            <p>例：String sql ="select ID,field from table where 1=1";</p>
                            <p>Object field = map.get("field");if(field!=null)</p>
                            <p>sql += " and field like \'%"+field+"%\'";</p>
                            <p>return sql; 其中的map为系统所封装的一个参数;</p>
                            <p>在脚本中使用map.get("field")可以获取表单提交时所携带的field参数值，脚本应拼接并返回任意的可执行的</p>
                            <p>sql语句.返回的sql中要含有ID</p>
                        </div>
                    </th>
                    <th>常用变量:</th>
                    <td>
                        <select name="initQuery" id="initQuery" lay-filter="initQuery">
                            <option value="">请选择</option>
                            <optgroup label="表字段" id="mainTableField"></optgroup>
                            <optgroup label="常用变量" id="commonVariable"></optgroup>
                        </select>
                    </td>
                    <th>表字段:</th>
                    <td>
                        <select name="tableField" id="tableField" lay-filter="tableField">
                            <option value="">请选择</option>
                        </select>
                    </td>
                    <th>表:</th>
                    <td>
                        <select name="initTable" id="initTable" lay-filter="initTable">
                            <option value=""></option>
                        </select>
                    </td>
                </tr>
            </table>
            <textarea id="textarea" cols="120" rows="10"></textarea>
        </div>
    </fieldset>
</form>
<script src="${ctx}/extends/jquery.min.js" type="text/javascript"></script>
<script>
    var setter;
    var tableId;
    var layer;
    var form;
    layui.config({
        base: serverPath.formPath + staticPath.formPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index', 'form', 'laydate', 'jquery', 'layer', 'setter', 'commons'], function () {
        form = layui.form;
        layer = layui.layer;
        var laydate = layui.laydate;
        setter = layui.setter;
        var $ = layui.$;
        var commons = layui.commons;
        tableId = '${tableId}';
        var add = '${add}';
        var selectAttrName = '${selectAttrName}';
        //数据回显
        $.ajax({
            url: serverPath.formPath + setter.formLinkPath +"formDef/filter",
            dataType: 'json',
            type: "post",
            data: {"tableId": tableId},
            success: function (result) {
                if(commons._filterRecord(result)){
                    var bpmFormTable = result.data.bpmFormTable;
                    //常用变量
                    var commonVariable="";
                    for(var i = 0; i < result.data.commonVars.length; i++){
                        commonVariable = commonVariable +'<option value="'+result.data.commonVars[i]['alias']+'">'+result.data.commonVars[i]['name']+'</option>\n'
                    }
                    $("#commonVariable").append(commonVariable);
                    $("#judgeCon-6").children("select").append(commonVariable);
                    var mainTableField="";
                    for(var i = 0; i < bpmFormTable.fieldList.length; i++){
                        mainTableField = mainTableField +'<option value="'+bpmFormTable.fieldList[i]['fieldName']+'">'+bpmFormTable.fieldList[i]['fieldDesc']+'</option>\n'
                    }
                    $("#mainTableField").append(mainTableField);

                    //表字段
                    var tableField = '<optgroup label="'+bpmFormTable.tableDesc+'">';
                    for(var i = 0; i < bpmFormTable.fieldList.length; i++){
                        tableField = tableField + '<option value="'+bpmFormTable.fieldList[i]['fieldName']+'">'+bpmFormTable.fieldList[i]['fieldDesc']+'</option>\n'
                    }
                    tableField = tableField + '</optgroup>';

                    for(var i = 0; i < bpmFormTable.subTableList.length; i++){
                        tableField = tableField + '<optgroup label="'+bpmFormTable.subTableList[i].tableDesc+'">';
                        for(var j = 0; j < bpmFormTable.subTableList[i].fieldList.length; j++){
                            tableField = tableField + '<option value="'+bpmFormTable.subTableList[i].fieldList[j]['fieldName']+'">'+bpmFormTable.subTableList[i].fieldList[j]['fieldDesc']+'</option>\n'
                        }
                        tableField = tableField + '</optgroup>';
                    }
                    for(var i = 0; i < bpmFormTable.otherTableList.length; i++){
                        tableField = tableField + '<optgroup label="'+bpmFormTable.otherTableList[i].tableDesc+'">';
                        for(var j = 0; j < bpmFormTable.otherTableList[i].fieldList.length; j++){
                            tableField = tableField +'<option value="'+bpmFormTable.otherTableList[i].fieldList[j]['fieldName']+'">'+bpmFormTable.otherTableList[i].fieldList[j]['fieldDesc']+'</option>\n'
                        }
                        tableField = tableField + '</optgroup>';
                    }

                    $("#tableField").append(tableField);
                    //表
                    var tableNames = '<option value="W_'+bpmFormTable.tableName+'">'+bpmFormTable.tableDesc+'</option>';
                    for(var i = 0; i < bpmFormTable.subTableList.length; i++){
                        tableNames = tableNames + '<option value="W_'+bpmFormTable.subTableList[i].tableName+'">'+bpmFormTable.subTableList[i].tableDesc+'</option>\n'
                    }
                    for(var i = 0; i < bpmFormTable.otherTableList.length; i++){
                        tableNames = tableNames + '<option value="'+bpmFormTable.otherTableList[i].tableName+'">'+bpmFormTable.otherTableList[i].tableDesc+'</option>\n'
                    }
                    $("#initTable").append(tableNames);
                    form.render('select');
                }
            }
        });
        // 如果为编辑  数据回显
        if(add==0){
            var result = parent.layui.getFilterField(selectAttrName);
            $("#filterKey").val(result.key);
            $("#filterName").val(result.name);
            $("#scriptType option[value="+result.type+"]").attr("selected",true);
            if(result.type==1){
                var condition = JSON.parse(result.condition);
                if(condition.length>0){
                    //回显 添加条件
                    $.ajax({
                        url: serverPath.formPath + setter.formLinkPath +"formDef/filter",
                        dataType: 'json',
                        type: "post",
                        data: {"tableId": tableId},
                        success: function (result) {
                            var formTable = result.data.bpmFormTable;
                            if (commons._filterRecord(result)) {
                                //表字段
                                var tableField = '<optgroup label="'+formTable.tableDesc+'">';
                                for(var i = 0; i < formTable.fieldList.length; i++){
                                    tableField = tableField + '<option aryOptions=\''+JSON.stringify(formTable.fieldList[i]['aryOptions'])+'\' table="'+formTable.tableName+'" maintable="'+formTable.tableName+'" relation="'+formTable.relation+'" source="'+result.data.source+'" ishidden="'+formTable.fieldList[i]['isHidden']+'" ftype="'+formTable.fieldList[i]['fieldType']+'" ctltype="'+formTable.fieldList[i]['controlType']+'" value="'+formTable.fieldList[i]['fieldName']+'">'+formTable.fieldList[i]['fieldDesc']+'</option>\n'
                                }
                                tableField = tableField + '</optgroup>';

                                for(var i = 0; i < formTable.subTableList.length; i++){
                                    tableField = tableField + '<optgroup label="'+formTable.subTableList[i].tableDesc+'">';
                                    for(var j = 0; j < formTable.subTableList[i].fieldList.length; j++){
                                        tableField = tableField + '<option aryOptions=\''+JSON.stringify(formTable.subTableList[i].fieldList[j]['aryOptions'])+'\' table="'+formTable.subTableList[i].tableName+'" maintable="'+formTable.tableName+'" relation="'+formTable.subTableList[i].relation+'" source="'+result.data.source+'" ishidden="'+formTable.subTableList[i].fieldList[j]['isHidden']+'" ftype="'+formTable.subTableList[i].fieldList[j]['fieldType']+'" ctltype="'+formTable.subTableList[i].fieldList[j]['controlType']+'" value="'+formTable.subTableList[i].fieldList[j]['fieldName']+'">'+formTable.subTableList[i].fieldList[j]['fieldDesc']+'</option>\n'
                                    }
                                    tableField = tableField + '</optgroup>';
                                }
                                for(var i = 0; i < formTable.otherTableList.length; i++){
                                    tableField = tableField + '<optgroup label="'+formTable.otherTableList[i].tableDesc+'">';
                                    for(var j = 0; j < formTable.otherTableList[i].fieldList.length; j++){
                                        tableField = tableField +'<option aryOptions=\''+JSON.stringify(formTable.otherTableList[i].fieldList[j]['aryOptions'])+'\' table="'+formTable.otherTableList[i].tableName+'" maintable="'+formTable.tableName+'" relation="'+formTable.otherTableList[i].relation+'" source="2" ishidden="'+formTable.otherTableList[i].fieldList[j]['isHidden']+'" ftype="'+formTable.otherTableList[i].fieldList[j]['fieldType']+'" ctltype="'+formTable.otherTableList[i].fieldList[j]['controlType']+'" judge="otherTable" value="'+formTable.otherTableList[i].fieldList[j]['fieldName']+'">'+formTable.otherTableList[i].fieldList[j]['fieldDesc']+'</option>\n'
                                    }
                                    tableField = tableField + '</optgroup>';
                                }

                                for(var k=0;k<condition.length;k++){
                                    if(condition[k].ruleType==2){
                                        //回显 添加脚本
                                        var uid = guid2();
                                        var script =
                                            "<div judge='jiaoben' class='layui-form-item scriptStyle' style='border: 1px solid #C0C4CC;position: relative; height: 37px; width: 90%;margin: 4px 0 4px 4px'>\n";
                                        if(k!=0){
                                            script = script +
                                                "    <span style='border-right: 1px solid #555;position: absolute;top: -30px;right: -30px;height: 10px;'></span>\n" +
                                                "    <span style='border-right: 1px solid #555;position: absolute;top: 0px;right: -30px;height: 15px;'></span>\n" +
                                                "    <span style='border-top: 1px solid #555;position: absolute;top: -30px;right: -30px;width: 30px;'></span>\n" +
                                                "    <span style='border-top: 1px solid #555;position: absolute;top: 14px;right: -30px;width: 30px;'></span>\n" +
                                                "    <select lay-ignore='' style='display: none;position: absolute;top: -20px;right: -50px;' id='selectAndOr"+k+"'>\n" +
                                                "        <option value='and'>且</option>\n" +
                                                "        <option value='or'>或</option>\n" +
                                                "    </select>\n";
                                        }
                                        script = script +
                                            "    <input name='RulesCK' type='checkbox' lay-skin='primary'>\n"+
                                            "    <div>\n"+
                                            "       <label class='layui-form-labe'>脚本条件</label>\n" +
                                            "       <a onclick='scriptCondition(this)' style='color: #0b73df;display: inline-block;margin-top:1%;margin-left:6px;'>脚本</a>\n" +
                                            "       <textarea id='"+uid+"' class='layui-input' style='display:none;'>"+JSON.stringify(condition[k])+"</textarea>\n" +
                                            "    </div>\n"+
                                            "</div>\n";
                                        $("#conditionalScript").append(script);
                                        $("#selectAndOr"+k+" option[value='"+condition[k].compType+"']").attr("selected",true);
                                    }else{
                                        var wid = guid2();
                                        var script =
                                            "<div judge='putong' class='layui-form-item addScriptStyle' style='border: 1px solid #C0C4CC; position:relative;height: 37px; width: 90%;margin: 4px 0 4px 4px'>\n";
                                        if(k!=0){
                                            script = script +
                                                "    <span style='border-right: 1px solid #555;position: absolute;top: -30px;right: -30px;height: 10px;'></span>\n" +
                                                "    <span style='border-right: 1px solid #555;position: absolute;top: 0px;right: -30px;height: 15px;'></span>\n" +
                                                "    <span style='border-top: 1px solid #555;position: absolute;top: -30px;right: -30px;width: 30px;'></span>\n" +
                                                "    <span style='border-top: 1px solid #555;position: absolute;top: 14px;right: -30px;width: 30px;'></span>\n" +
                                                "    <select lay-ignore='' style='position: absolute;top: -20px;right: -50px;' id='selectAndOr"+k+"'>\n" +
                                                "        <option value='and'>且</option>\n" +
                                                "        <option value='or'>或</option>\n" +
                                                "    </select>\n";
                                        }
                                        script = script +
                                            "    <input name='RulesCK' type='checkbox' lay-skin='primary'>"+
                                            "    <div style='float: left'>\n" +
                                            "        <label class='layui-form-labe' >普通条件</label>\n" +
                                            "        <div class='layui-input-block' style='line-height: 36px;'>\n" +
                                            "              <select id='append"+wid+"' lay-ignore='' onchange='conditionSelectChange(this)' lay-verify='required' style='vertical-align: text-bottom;width: 132px;float: left;margin-top:6px;' class='someRule'>\n" +
                                            "                    <option value=''>--请选择--</option>\n" +
                                            "              </select>\n" +
                                            "        </div>\n" +
                                            "    </div>\n" +
                                            "</div>";
                                        $("#conditionalScript").append(script);
                                        $("#selectAndOr"+k+" option[value='"+condition[k].compType+"']").attr("selected",true);
                                        $("#append"+wid).append(tableField);
                                        $("#append"+wid+" option[value='"+condition[k].flowvarKey+"']").attr("selected",true);
                                        conditionSelectFieldDisplay(document.getElementById("append"+wid),condition[k]);
                                        form.render('select');
                                    }
                                }
                                form.render();
                            }
                        }
                    });
                }
            }else{
                $("#textarea").val(JSON.parse(result.condition));
                $("#sqlSet").show();
                $("#conditionSetting").hide();
            }
        }

        //数据回显 在普通规则 表字段回显的方法
        function conditionSelectFieldDisplay(elem,commonRule){
            var selectElement = $(elem);
            var selectId = $(elem).attr("id");
            var attrValue = $(elem).find("option:selected").attr("judge");
            var selectFtype = $(elem).find("option:selected").attr("ftype");
            var selectValue = $(elem).find("option:selected").val();//选择的select的值
            var selectControlType = $(elem).find("option:selected").attr("ctltype");
            var optType = getOptType($(elem).find("option:selected"));
            var selectAryOptions = $(elem).find("option:selected").attr("aryOptions");
            if(attrValue==undefined){
                if(optType==2){//字符串类型
                    selectElement.nextAll().remove();//先删除后面的所有兄弟元素
                    var clone = $("#judgeCon-2").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);

                    selectElement.next().find("select option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    selectElement.next().find("input").val(commonRule.judgeVal1);

                }else if(optType==3){//日期类型
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-3").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);

                    $(selectElement.next().find("select")[0]).find("option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    $(selectElement.next().find("input")[0]).val(commonRule.judgeVal1);
                    $(selectElement.next().find("select")[1]).find("option[value='"+commonRule.judgeCon2+"']").attr("selected",true);
                    $(selectElement.next().find("input")[1]).val(commonRule.judgeVal2);

                }else if(optType==1){//数字类型
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-1").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);

                    $(selectElement.next().find("select")[0]).find("option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    $(selectElement.next().find("input")[0]).val(commonRule.judgeVal1);
                    $(selectElement.next().find("select")[1]).find("option[value='"+commonRule.judgeCon2+"']").attr("selected",true);
                    $(selectElement.next().find("input")[1]).val(commonRule.judgeVal2);

                }else if(optType==4) {//数据字典、下拉选项、复选框、单选按钮
                    if(selectControlType!=3){//除了数据字典
                        selectElement.nextAll().remove();
                        var clone = $("#judgeCon-4").clone();
                        clone.removeAttr("id");
                        clone.find("option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                        selectElement.after(clone);
                    }else{
                        selectElement.nextAll().remove();
                    }

                    if(selectControlType==11){//下拉选项
                        var cloneXiaLa = $("#judgeCon-10").clone();
                        cloneXiaLa.removeAttr("id");
                        $.each(JSON.parse(selectAryOptions), function(key) {
                            cloneXiaLa.find("select").append('<option value="'+key+'">'+JSON.parse(selectAryOptions)[key]+'</option>');
                        });
                        cloneXiaLa.find("option[value='"+commonRule.judgeVal1+"']").attr("selected",true);
                        selectElement.parent().append(cloneXiaLa)

                    }else if(selectControlType==13){//复选
                        var cloneFuXuan = $("#judgeCon-11").clone();
                        cloneFuXuan.removeAttr("id");

                        $.each(JSON.parse(selectAryOptions), function(key) {
                            cloneFuXuan.append('<input type="checkbox" name="" value="'+key+'" title="'+JSON.parse(selectAryOptions)[key]+'" lay-skin="primary"/>');
                        });
                        if(commonRule.judgeVal1!=""){
                            var idsAndNamesArray = (commonRule.judgeVal1).split("&amp;&amp;");
                            $.each(idsAndNamesArray, function(i) {
                                cloneFuXuan.find("input:checkbox[value='"+idsAndNamesArray[i]+"']").prop("checked", "checked");
                            });
                        }
                        selectElement.parent().append(cloneFuXuan);
                        form.render("checkbox");

                    }else if(selectControlType==14){//单选
                        var cloneDanXuan = $("#judgeCon-12").clone();
                        cloneDanXuan.removeAttr("id");
                        $.each(JSON.parse(selectAryOptions), function(key) {
                            cloneDanXuan.append('<input type="radio" name="" value="'+key+'" title="'+JSON.parse(selectAryOptions)[key]+'"/>');
                        });
                        if(commonRule.judgeVal1!=""){
                            cloneDanXuan.find("input:radio[value='"+commonRule.judgeVal1+"']").prop("checked", "checked");
                        }
                        selectElement.parent().append(cloneDanXuan);
                        form.render("radio");
                    }
                }else if(optType==5) {

                    selectElement.nextAll().remove();
                    var clone5 = $("#judgeCon-5").clone();
                    clone5.removeAttr("id");
                    selectElement.after(clone5);

                    selectElement.next().find("select option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    if(commonRule.judgeCon1=="1"||commonRule.judgeCon1=="2") {

                        var clone;
                        if (selectControlType == 4 || selectControlType == 8) {//人员
                            clone = $("#judgeCon-7").clone();
                        } else if (selectControlType == 5 || selectControlType == 17) {//角色
                            clone = $("#judgeCon-9").clone();
                        } else if (selectControlType == 6 || selectControlType == 18) {//组织
                            clone = $("#judgeCon-8").clone();
                        } else if (selectControlType == 7 || selectControlType == 19) {//岗位
                            clone = $("#judgeCon-13").clone();
                        }
                        clone.removeAttr("id");
                        $(clone.children().get(0)).attr("id", "names" + selectId + "filter");
                        $(clone.children().get(1)).attr("inputIndex", selectId + "filter");
                        $(clone.children().get(2)).attr("id", "ids" + selectId + "filter");
                        selectElement.parent().append(clone);
                        var idsAndNamesArray = [];
                        if (commonRule.judgeVal1 != "") {
                            idsAndNamesArray = (commonRule.judgeVal1).split("&amp;&amp;");
                            $(selectElement.next().next().find("input")[0]).val(idsAndNamesArray[1]);
                            $(selectElement.next().next().find("input")[1]).val(idsAndNamesArray[0]);
                        }
                    }
                }else{
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-2").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);
                    selectElement.next().find("select option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    selectElement.next().find("input").val(commonRule.judgeVal1);
                }
            }else{
                if(selectValue=='BUS_CREATOR_ID'||selectValue=='BUS_ORG_ID'||selectValue=='BUS_UPDID'){//发起人id
                    selectElement.nextAll().remove();
                    var clone5 = $("#judgeCon-5").clone();
                    clone5.removeAttr("id");
                    selectElement.after(clone5);

                    selectElement.next().find("select option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    if(commonRule.judgeCon1=="1"||commonRule.judgeCon1=="2"){

                        var clone;
                        if(selectValue=='BUS_ORG_ID'){
                            clone = $("#judgeCon-8").clone();
                        }else{
                            clone = $("#judgeCon-7").clone();
                        }
                        clone.removeAttr("id");
                        $(clone.children().get(0)).attr("id","names"+selectId+"filter");
                        $(clone.children().get(1)).attr("inputIndex",selectId+"filter");
                        $(clone.children().get(2)).attr("id","ids"+selectId+"filter");
                        selectElement.parent().append(clone);
                        var idsAndNamesArray = [];
                        if(commonRule.judgeVal1!=""){
                            idsAndNamesArray = (commonRule.judgeVal1).split("&amp;&amp;");
                            $(selectElement.next().next().find("input")[0]).val(idsAndNamesArray[1]);
                            $(selectElement.next().next().find("input")[1]).val(idsAndNamesArray[0]);
                        }
                    }else{
                        var clone = $("#judgeCon-6").clone();
                        clone.removeAttr("id");
                        selectElement.parent().append(clone);
                        selectElement.next().next().find("select option[value='"+commonRule.judgeVal1+"']").attr("selected",true);
                    }

                }else if(selectValue=='BUS_CREATETIME'||selectValue=='BUS_UPDTIME'){//发起时间、更新时间
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-3").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);
                    $(selectElement.next().find("select")[0]).find("option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    $(selectElement.next().find("input")[0]).val(commonRule.judgeVal1);
                    $(selectElement.next().find("select")[1]).find("option[value='"+commonRule.judgeCon2+"']").attr("selected",true);
                    $(selectElement.next().find("input")[1]).val(commonRule.judgeVal2);
                }else if(selectValue=='BUS_STATUS'){//状态
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-1").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);
                    $(selectElement.next().find("select")[0]).find("option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    $(selectElement.next().find("input")[0]).val(commonRule.judgeVal1);
                    $(selectElement.next().find("select")[1]).find("option[value='"+commonRule.judgeCon2+"']").attr("selected",true);
                    $(selectElement.next().find("input")[1]).val(commonRule.judgeVal2);
                }else{
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-2").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);
                    selectElement.next().find("select option[value='"+commonRule.judgeCon1+"']").attr("selected",true);
                    selectElement.next().find("input").val(commonRule.judgeVal1);
                }
            }
        }



        //监听select
        form.on('select(initQuery)', function(data){
            //将数据插入到光标处
            $("#textarea").insertAtCaret(data.value);
            $("#textarea").focus();
            $("#initQuery").val("");
            form.render('select');
        });
        form.on('select(tableField)', function(data){
            //将数据插入到光标处
            $("#textarea").insertAtCaret(data.value);
            $("#textarea").focus();
            $("#tableField").val("");
            form.render('select');
        });
        form.on('select(initTable)', function(data){
            //将数据插入到光标处
            $("#textarea").insertAtCaret(data.value);
            $("#textarea").focus();
            $("#initTable").val("");
            form.render('select');
        });

        form.on('select(scriptType)', function(data){
           if(data.value==2){
                $("#sqlSet").show();
                $("#conditionSetting").hide();
           }else{
               $("#sqlSet").hide();
               $("#conditionSetting").show();
           }
        });
        //添加 修改按钮
        form.on('submit(addCondition)', function (data) {
            var filterName = $("#filterName").val();
            var filterKey = $("#filterKey").val();
            var selectValue = $("#scriptType").find("option:selected").val();//选择的select的值
            var selectText = $("#scriptType").find("option:selected").text();//选择的select的文本
            var condition;
            if(selectValue==2){
                condition = $("#textarea").val();
            }else{
                condition = [];
                var count = $("#conditionalScript").children().length;
                for(var i=0;i<count;i++){
                    var judge = $($("#conditionalScript").children()[i]).attr("judge");
                    //判断 脚本条件 还是 普通条件
                    if(judge=='jiaoben'){
                        var value = $($("#conditionalScript").children()[i]).find('textarea').val();
                        var jsonArray = JSON.parse(value);
                        if(i!=0){
                            var connect = $($($("#conditionalScript").children()[i]).find('select')[0]).find("option:selected").val();
                            jsonArray.compType = connect;
                        }
                        condition.push(jsonArray);
                    }else{
                        var fieldSelect;
                        var json = {};
                        if(i!=0){
                            fieldSelect = $($($("#conditionalScript").children()[i]).find('select')[1]);
                            var connect = $($($("#conditionalScript").children()[i]).find('select')[0]).find("option:selected").val();
                            json.compType = connect;
                        }else{
                            fieldSelect = $($($("#conditionalScript").children()[i]).find('select')[0]);
                        }
                        var source = fieldSelect.find("option:selected").attr("source");
                        json.source = source;
                        var table = fieldSelect.find("option:selected").attr("table");
                        json.table = table;
                        var mainTable = fieldSelect.find("option:selected").attr("maintable");
                        json.mainTable = mainTable;
                        var relation = fieldSelect.find("option:selected").attr("relation");
                        json.relation = relation;
                        var isHidden = fieldSelect.find("option:selected").attr("ishidden");
                        json.isHidden = isHidden;
                        var flowvarKey = fieldSelect.find("option:selected").val();
                        var flowvarKeyText = fieldSelect.find("option:selected").text();
                        json.flowvarKey = flowvarKey;
                        var ctltype = fieldSelect.find("option:selected").attr("ctltype");
                        var optType = getOptType(fieldSelect.find("option:selected"));
                        json.optType = optType;
                        var ruleType = '1';
                        json.ruleType = ruleType;

                        var attrValue = fieldSelect.find("option:selected").attr("judge");//select 的 judge属性值
                        var selectFtype = fieldSelect.find("option:selected").attr("ftype");//select 的 ftype属性值
                        var select1Value = fieldSelect.find("option:selected").val();
                        var selectControlType = fieldSelect.find("option:selected").attr("ctltype");
                        var optType = getOptType(fieldSelect.find("option:selected"));

                        if(attrValue==undefined){
                            if(optType==2){//字符串类型
                                var judgeCon1 = fieldSelect.next().find("select").find("option:selected").val();
                                var judgeCon1Text = fieldSelect.next().find("select").find("option:selected").text();
                                var judgeVal1 = fieldSelect.next().find("input").val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1;
                            }else if(optType==3){//日期类型
                                var judgeCon1 = $(fieldSelect.next().find("select")[0]).find("option:selected").val();
                                var judgeCon1Text = $(fieldSelect.next().find("select")[0]).find("option:selected").text();
                                var judgeVal1 = $(fieldSelect.next().find("input")[0]).val();
                                var judgeCon2 = $(fieldSelect.next().find("select")[1]).find("option:selected").val();
                                var judgeCon2Text = $(fieldSelect.next().find("select")[1]).find("option:selected").text();
                                var judgeVal2 = $(fieldSelect.next().find("input")[1]).val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.judgeCon2 = judgeCon2;
                                json.judgeVal2 = judgeVal2;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1+"并且"+judgeCon2Text+" "+judgeVal2;
                            }else if(optType==1){//数字类型
                                var judgeCon1 = $(fieldSelect.next().find("select")[0]).find("option:selected").val();
                                var judgeCon1Text = $(fieldSelect.next().find("select")[0]).find("option:selected").text();
                                var judgeVal1 = $(fieldSelect.next().find("input")[0]).val();
                                var judgeCon2 = $(fieldSelect.next().find("select")[1]).find("option:selected").val();
                                var judgeCon2Text = $(fieldSelect.next().find("select")[1]).find("option:selected").text();
                                var judgeVal2 = $(fieldSelect.next().find("input")[1]).val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.judgeCon2 = judgeCon2;
                                json.judgeVal2 = judgeVal2;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1+"并且"+judgeCon2Text+" "+judgeVal2;
                            }else if(optType==4){//复选、单选、下拉选项、数据字典
                                if(selectControlType==11){//下拉选项
                                    var judgeCon = fieldSelect.next().find("select").find("option:selected");
                                    var judgeCon1 = judgeCon.val();
                                    var judgeCon1Text = judgeCon.text();
                                    var judgeVal = fieldSelect.next().next().find("select").find("option:selected");
                                    var judgeVal1 = judgeVal.val();
                                    var judgeVal1Text = judgeVal.text();
                                    json.judgeCon1 = judgeCon1;
                                    json.judgeVal1 = judgeVal1;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1Text;
                                }else if(selectControlType==13){//复选
                                    var judgeCon = fieldSelect.next().find("select").find("option:selected");
                                    var judgeCon1 = judgeCon.val();
                                    var judgeCon1Text = judgeCon.text();

                                    var checkedArray = fieldSelect.next().next().find("input[type=checkbox]:checked");
                                    var judgeVal1 = "";
                                    for(var a=0;a<checkedArray.length;a++){
                                        if(a==0){
                                            judgeVal1 = judgeVal1+$(checkedArray[a]).val();
                                        }else{
                                            judgeVal1 = judgeVal1+"&&"+$(checkedArray[a]).val();
                                        }
                                    }
                                    var judgeVal1Text = "";
                                    for(var j=0;j<checkedArray.length;j++){
                                        if(j==0){
                                            judgeVal1Text = judgeVal1Text+$(checkedArray[j]).attr("title");
                                        }else{
                                            judgeVal1Text = judgeVal1Text+"&&"+$(checkedArray[j]).attr("title");
                                        }
                                    }
                                    json.judgeCon1 = judgeCon1;
                                    json.judgeVal1 = judgeVal1;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1Text;
                                }else if(selectControlType==14){//单选
                                    var judgeCon1 = fieldSelect.next().find("select").find("option:selected").val();
                                    var judgeCon1Text = fieldSelect.next().find("select").find("option:selected").text();

                                    var checkedArray = fieldSelect.next().next().find("input[type=radio]:checked");
                                    var judgeVal1 = "";
                                    if(checkedArray.length>0){
                                        judgeVal1 = $(checkedArray[0]).val();
                                    }
                                    var judgeVal1Text = "";
                                    if(checkedArray.length>0){
                                        judgeVal1Text = $(checkedArray[0]).attr("title");
                                    }
                                    json.judgeCon1 = judgeCon1;
                                    json.judgeVal1 = judgeVal1;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1Text;
                                }
                            }else if(optType==5){

                                var judgeCon1 = fieldSelect.next().find("select").find("option:selected").val();
                                json.judgeCon1 = judgeCon1;
                                var judgeCon1Text = fieldSelect.next().find("select").find("option:selected").text();
                                if(judgeCon1=="1"||judgeCon1=="2"){
                                    var names = $(fieldSelect.next().next().find("input")[0]).val();
                                    var ids = $(fieldSelect.next().next().find("input")[1]).val();
                                    json.judgeVal1 = ids+"&&"+names;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+names;
                                }else{
                                    var CommonVarValue = fieldSelect.next().next().find("select").find("option:selected").val();
                                    var CommonVarText = fieldSelect.next().next().find("select").find("option:selected").text();
                                    json.judgeVal1 = CommonVarValue;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+CommonVarText;
                                }

                            }else{//按字符串处理
                                var judgeCon1 = fieldSelect.next().find("select").find("option:selected").val();
                                var judgeCon1Text = fieldSelect.next().find("select").find("option:selected").text();
                                var judgeVal1 = fieldSelect.next().find("input").val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1;
                            }
                        }else{
                            if(select1Value=='BUS_CREATOR_ID'||select1Value=='BUS_ORG_ID'||select1Value=='BUS_UPDID'){//发起人id

                                var judgeCon1 = fieldSelect.next().find("select").find("option:selected").val();
                                json.judgeCon1 = judgeCon1;
                                var judgeCon1Text = fieldSelect.next().find("select").find("option:selected").text();
                                if(judgeCon1=="1"||judgeCon1=="2"){
                                    var names = $(fieldSelect.next().next().find("input")[0]).val();
                                    var ids = $(fieldSelect.next().next().find("input")[1]).val();
                                    json.judgeVal1 = ids+"&&"+names;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+names;
                                }else{
                                    var CommonVarValue = fieldSelect.next().next().find("select").find("option:selected").val();
                                    var CommonVarText = fieldSelect.next().next().find("select").find("option:selected").text();
                                    json.judgeVal1 = CommonVarValue;
                                    json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+CommonVarText;
                                }

                            }else if(select1Value=='BUS_CREATETIME'||select1Value=='BUS_UPDTIME'){//发起时间、更新时间
                                var judgeCon1 = $(fieldSelect.next().find("select")[0]).find("option:selected").val();
                                var judgeCon1Text = $(fieldSelect.next().find("select")[0]).find("option:selected").text();
                                var judgeVal1 = $(fieldSelect.next().find("input")[0]).val();
                                var judgeCon2 = $(fieldSelect.next().find("select")[1]).find("option:selected").val();
                                var judgeCon2Text = $(fieldSelect.next().find("select")[1]).find("option:selected").text();
                                var judgeVal2 = $(fieldSelect.next().find("input")[1]).val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.judgeCon2 = judgeCon2;
                                json.judgeVal2 = judgeVal2;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1+"并且"+judgeCon2Text+" "+judgeVal2;
                            }else if(select1Value=='BUS_STATUS'){//状态
                                var judgeCon1 = $(fieldSelect.next().find("select")[0]).find("option:selected").val();
                                var judgeCon1Text = $(fieldSelect.next().find("select")[0]).find("option:selected").text();
                                var judgeVal1 = $(fieldSelect.next().find("input")[0]).val();
                                var judgeCon2 = $(fieldSelect.next().find("select")[1]).find("option:selected").val();
                                var judgeCon2Text = $(fieldSelect.next().find("select")[1]).find("option:selected").text();
                                var judgeVal2 = $(fieldSelect.next().find("input")[1]).val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.judgeCon2 = judgeCon2;
                                json.judgeVal2 = judgeVal2;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1+"并且"+judgeCon2Text+" "+judgeVal2;
                            }else{//按字符串处理
                                var judgeCon1 = fieldSelect.next().find("select").find("option:selected").val();
                                var judgeCon1Text = fieldSelect.next().find("select").find("option:selected").text();
                                var judgeVal1 = fieldSelect.next().find("input").val();
                                json.judgeCon1 = judgeCon1;
                                json.judgeVal1 = judgeVal1;
                                json.conDesc = flowvarKeyText+" "+judgeCon1Text+" "+judgeVal1;
                            }
                        }
                        condition.push(json);
                    }
                }
                condition = JSON.stringify(condition);
            }
            var result = {"name":filterName,"key":filterKey,"type":selectText,"condition":condition,"right":[{"type":"none"}]};
            //修改
            if(add==0){
                parent.layui.EditFilterField(selectAttrName,result);
            }else {
                parent.layui.AddFilterField(result);
            }
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });

        //添加子页面的数据
        parent.window.addScriptJson=function(scriptJson,uuid){
            var childrenLength = $("#conditionalScript").children().length;
            var script =
                "<div judge='jiaoben' class='layui-form-item scriptStyle' style='border: 1px solid #C0C4CC;position: relative; height: 37px; width: 90%;margin: 4px 0 4px 4px'>\n";
            if(childrenLength > 0){
                script = script +
                    "    <span style='border-right: 1px solid #555;position: absolute;top: -30px;right: -30px;height: 10px;'></span>\n" +
                    "    <span style='border-right: 1px solid #555;position: absolute;top: 0px;right: -30px;height: 15px;'></span>\n" +
                    "    <span style='border-top: 1px solid #555;position: absolute;top: -30px;right: -30px;width: 30px;'></span>\n" +
                    "    <span style='border-top: 1px solid #555;position: absolute;top: 14px;right: -30px;width: 30px;'></span>\n" +
                    "    <select lay-ignore='' style='position: absolute;top: -20px;right: -50px;'>\n" +
                    "        <option value='and'>且</option>\n" +
                    "        <option value='or'>或</option>\n" +
                    "    </select>\n";
            }
            script = script +
                "    <input name='RulesCK' type='checkbox' lay-skin='primary'>\n"+
                "    <div>\n"+
                "       <label class='layui-form-labe'>脚本条件</label>\n" +
                "       <a onclick='scriptCondition(this)' style='color: #0b73df;display: inline-block;margin-top:1%;margin-left:6px;'>脚本</a>\n" +
                "       <textarea id='"+uuid+"' class='layui-input' style='display:none;'>"+JSON.stringify(scriptJson)+"</textarea>\n" +
                "    </div>\n"+
                "</div>\n";
            $("#conditionalScript").append(script);
            form.render()
        };
        //修改子页面的数据
        parent.window.editScriptJson=function(scriptJson,id){
            $("#"+id).val(JSON.stringify(scriptJson));
        };

        //获取textarea的值
        parent.window.getTextAreaValue=function(id){
            return $("#"+id).val();
        };
        //删除 普通条件和脚本条件
        $("#deleteCondition").on('click',function(){
            $("input:checkbox[name='RulesCK']:checked").each(function () {
                $(this).parent().remove();
            });
            $($("#conditionalScript").children()[0]).children('span').remove();
            $($("#conditionalScript").children()[0]).children('select').remove();
        });

        //添加脚本弹窗
        $("#addScript").on('click',function(){
            parent.layer.open({
                type: 2,
                title: "脚本窗口",
                shade: 0.3,
                area: ['750px', '420px'],
                content: serverPath.formPath + setter.formLinkPath +'formDef/view/filterScript?tableId='+tableId+'&add=1'
            })
        });
        //添加条件
        $("#addTerm").on('click',function(){
            var childrenLength = $("#conditionalScript").children().length;
            var selectId = guid2();
            var str =
                "<div judge='putong' class='layui-form-item addScriptStyle' style='border: 1px solid #C0C4CC; position:relative;height: 37px; width: 90%;margin: 4px 0 4px 4px'>\n";
            if(childrenLength > 0){
                str = str +
                "    <span style='border-right: 1px solid #555;position: absolute;top: -30px;right: -30px;height: 10px;'></span>\n" +
                "    <span style='border-right: 1px solid #555;position: absolute;top: 0px;right: -30px;height: 15px;'></span>\n" +
                "    <span style='border-top: 1px solid #555;position: absolute;top: -30px;right: -30px;width: 30px;'></span>\n" +
                "    <span style='border-top: 1px solid #555;position: absolute;top: 14px;right: -30px;width: 30px;'></span>\n" +
                "    <select lay-ignore='' style='position: absolute;top: -20px;right: -50px;'>\n" +
                "        <option value='and'>且</option>\n" +
                "        <option value='or'>或</option>\n" +
                "    </select>\n";
            }
            str = str +
                "    <input name='RulesCK' type='checkbox' lay-skin='primary'>"+
                "    <div style='float: left'>\n" +
                "        <label class='layui-form-labe' >普通条件</label>\n" +
                "        <div class='layui-input-block' style='line-height: 36px;'>\n" +
                "              <select id='"+selectId+"' lay-ignore='' style='vertical-align: text-bottom;width:132px;float: left;margin-top:6px;' onchange='conditionSelectChange(this)' class='someRule'>\n" +
                "                    <option value=''>--请选择--</option>\n" +
                "              </select>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</div>";
            $("#conditionalScript").append(str);
            //数据回显 拼接表字段
            $.ajax({
                url: serverPath.formPath + setter.formLinkPath +"formDef/filter",
                dataType: 'json',
                type: "post",
                data: {"tableId": tableId},
                success: function (result) {
                    if(commons._filterRecord(result)){
                        var bFormTable = result.data.bpmFormTable;
                        //表字段
                        var tableField = '<optgroup label="'+bFormTable.tableDesc+'">';
                        for(var i = 0; i < bFormTable.fieldList.length; i++){
                            tableField = tableField + '<option aryOptions=\''+JSON.stringify(bFormTable.fieldList[i]['aryOptions'])+'\' table="'+bFormTable.tableName+'" maintable="'+bFormTable.tableName+'" relation="'+bFormTable.relation+'" source="'+result.data.source+'" ishidden="'+bFormTable.fieldList[i]['isHidden']+'" ftype="'+bFormTable.fieldList[i]['fieldType']+'" ctltype="'+bFormTable.fieldList[i]['controlType']+'" value="'+bFormTable.fieldList[i]['fieldName']+'">'+bFormTable.fieldList[i]['fieldDesc']+'</option>\n'
                        }
                        tableField = tableField + '</optgroup>';

                        for(var i = 0; i < bFormTable.subTableList.length; i++){
                            tableField = tableField + '<optgroup label="'+bFormTable.subTableList[i].tableDesc+'">';
                            for(var j = 0; j < bFormTable.subTableList[i].fieldList.length; j++){
                                tableField = tableField + '<option aryOptions=\''+JSON.stringify(bFormTable.subTableList[i].fieldList[j]['aryOptions'])+'\' table="'+bFormTable.subTableList[i].tableName+'" maintable="'+bFormTable.tableName+'" relation="'+bFormTable.subTableList[i].relation+'" source="'+result.data.source+'" ishidden="'+bFormTable.subTableList[i].fieldList[j]['isHidden']+'" ftype="'+bFormTable.subTableList[i].fieldList[j]['fieldType']+'" ctltype="'+bFormTable.subTableList[i].fieldList[j]['controlType']+'" value="'+bFormTable.subTableList[i].fieldList[j]['fieldName']+'">'+bFormTable.subTableList[i].fieldList[j]['fieldDesc']+'</option>\n'
                            }
                            tableField = tableField + '</optgroup>';
                        }
                        for(var i = 0; i < bFormTable.otherTableList.length; i++){
                            tableField = tableField + '<optgroup label="'+bFormTable.otherTableList[i].tableDesc+'">';
                            for(var j = 0; j < bFormTable.otherTableList[i].fieldList.length; j++){
                                tableField = tableField +'<option aryOptions=\''+JSON.stringify(bFormTable.otherTableList[i].fieldList[j]['aryOptions'])+'\' table="'+bFormTable.otherTableList[i].tableName+'" maintable="'+bFormTable.tableName+'" relation="'+bFormTable.otherTableList[i].relation+'" source="2" ishidden="'+bFormTable.otherTableList[i].fieldList[j]['isHidden']+'" ftype="'+bFormTable.otherTableList[i].fieldList[j]['fieldType']+'" ctltype="'+bFormTable.otherTableList[i].fieldList[j]['controlType']+'" judge="otherTable" value="'+bFormTable.otherTableList[i].fieldList[j]['fieldName']+'">'+bFormTable.otherTableList[i].fieldList[j]['fieldDesc']+'</option>\n'
                            }
                            tableField = tableField + '</optgroup>';
                        }
                        $("#"+selectId).append(tableField);
                        form.render('select');
                    }
                }
            });
            form.render()
        });

        form.render();
        //初始化日期
        laydate.render({
            elem: '.renderDate'
        });

        $(".layui-icon-help").mouseover(function(){
            $(".help").show();
        });
        $(".layui-icon-help").mouseout(function(){
            $(".help").hide();
        });

        $("#cancel").click(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
    });

    /**
     * 获取字段类型类型识别码
     *	1、 数字标识
     *	2、字符串标识
     *	3、 日期标识
     *	4、数据字典标识
     *  5、选择器标识
     *
     */
    function getOptType(opt){
        var value = opt.val(),
            ctltype = opt.attr("ctltype"),
            ftype = opt.attr("ftype");

        //首先判断控件类型
        if(ctltype){
            switch(ctltype.toString()){
                //用户、角色、岗位、组织选择器
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "17":
                case "18":
                case "19":
                    return 5;
                //字典
                case "3":
                case "11":
                case "13":
                case "14":
                    return 4;
                //日期
                case "15":
                    return 3;
            }
        }
        //发起人
        if(value=="startUser")
            return 5;
        if(ftype){
            //根据变量类型识别
            switch(ftype.toLowerCase()){
                case "int":
                case "number":
                    return 1;
                case "varchar":
                case "string":
                    return 2;
                case "date":
                    return 3;
            }
        }
        return 2;
    }

    //监听普通条件 select
    function conditionSelectChange(elem){
        var selectElement = $(elem);
        var selectId = $(elem).attr("id");
        var attrValue = $(elem).find("option:selected").attr("judge");
        var selectControlType = $(elem).find("option:selected").attr("ctltype");
        var selectFtype = $(elem).find("option:selected").attr("ftype");
        var selectAryOptions = $(elem).find("option:selected").attr("aryOptions");
        var selectValue = $(elem).find("option:selected").val();//选择的select的值
        var optType = getOptType($(elem).find("option:selected"));
        if(attrValue==undefined){
            if(optType==2){//字符串类型或默认
                selectElement.nextAll().remove();//先删除后面的所有兄弟元素
                var clone = $("#judgeCon-2").clone();
                clone.removeAttr("id");
                selectElement.after(clone);

            }else if(optType==1){//数字类型
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-1").clone();
                clone.removeAttr("id");
                selectElement.after(clone);

            }else if(optType==3){//日期类型
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-3").clone();
                clone.removeAttr("id");
                selectElement.after(clone);

            }else if(optType==4){//复选、单选、下拉选项、数据字典
                if(selectControlType!=3){//除了数据字典
                    selectElement.nextAll().remove();
                    var clone = $("#judgeCon-4").clone();
                    clone.removeAttr("id");
                    selectElement.after(clone);
                }else{
                    selectElement.nextAll().remove();
                }

                if(selectControlType==11){//下拉选项
                    var cloneXiaLa = $("#judgeCon-10").clone();
                    cloneXiaLa.removeAttr("id");
                    $.each(JSON.parse(selectAryOptions), function(key) {
                        cloneXiaLa.find("select").append('<option value="'+key+'">'+JSON.parse(selectAryOptions)[key]+'</option>');
                    });
                    selectElement.parent().append(cloneXiaLa)

                }else if(selectControlType==13){//复选
                    var cloneFuXuan = $("#judgeCon-11").clone();
                    cloneFuXuan.removeAttr("id");
                    $.each(JSON.parse(selectAryOptions), function(key) {
                        cloneFuXuan.append('<input type="checkbox" name="" value="'+key+'" title="'+JSON.parse(selectAryOptions)[key]+'" lay-skin="primary"/>');
                    });
                    selectElement.parent().append(cloneFuXuan);
                    form.render("checkbox");

                }else if(selectControlType==14){//单选
                    var cloneDanXuan = $("#judgeCon-12").clone();
                    cloneDanXuan.removeAttr("id");
                    $.each(JSON.parse(selectAryOptions), function(key) {
                        cloneDanXuan.append('<input type="radio" name="" value="'+key+'" title="'+JSON.parse(selectAryOptions)[key]+'"/>');
                    });
                    selectElement.parent().append(cloneDanXuan);
                    form.render("radio");
                }

            }else if(optType==5){//各种选择器

                selectElement.nextAll().remove();
                var clone5 = $("#judgeCon-5").clone();
                clone5.removeAttr("id");
                selectElement.after(clone5);

                var clone;
                if(selectControlType==4||selectControlType==8){//人员
                    clone = $("#judgeCon-7").clone();
                }else if(selectControlType==5||selectControlType==17){//角色
                    clone = $("#judgeCon-9").clone();
                }else if(selectControlType==6||selectControlType==18){//组织
                    clone = $("#judgeCon-8").clone();
                }else if(selectControlType==7||selectControlType==19){//岗位
                    clone = $("#judgeCon-13").clone();
                }

                if(clone!=null){
                    clone.removeAttr("id");
                    $(clone.children().get(0)).attr("id","names"+selectId+"filter");
                    $(clone.children().get(1)).attr("inputIndex",selectId+"filter");
                    $(clone.children().get(2)).attr("id","ids"+selectId+"filter");
                    selectElement.parent().append(clone);
                }

            }else{
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-2").clone();
                clone.removeAttr("id");
                selectElement.after(clone);
            }
        }else{
            if(selectValue=='BUS_CREATOR_ID'){//发起人id
                selectElement.nextAll().remove();
                var clone5 = $("#judgeCon-5").clone();
                clone5.removeAttr("id");
                selectElement.after(clone5);

                var clone = $("#judgeCon-7").clone();
                clone.removeAttr("id");
                $(clone.children().get(0)).attr("id","names"+selectId+"filter");
                $(clone.children().get(1)).attr("inputIndex",selectId+"filter");
                $(clone.children().get(2)).attr("id","ids"+selectId+"filter");

                selectElement.parent().append(clone);
            }else if(selectValue=='BUS_ORG_ID'){//发起组织id
                selectElement.nextAll().remove();
                var clone5 = $("#judgeCon-5").clone();
                clone5.removeAttr("id");
                selectElement.after(clone5);

                var clone = $("#judgeCon-8").clone();
                clone.removeAttr("id");
                $(clone.children().get(0)).attr("id","names"+selectId+"filter");
                $(clone.children().get(1)).attr("inputIndex",selectId+"filter");
                $(clone.children().get(2)).attr("id","ids"+selectId+"filter");

                selectElement.parent().append(clone);
            }else if(selectValue=='BUS_CREATETIME'){//发起时间
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-3").clone();
                clone.removeAttr("id");
                selectElement.after(clone);
            }else if(selectValue=='BUS_UPDID'){//更新人id
                selectElement.nextAll().remove();
                var clone5 = $("#judgeCon-5").clone();
                clone5.removeAttr("id");
                selectElement.after(clone5);

                var clone = $("#judgeCon-7").clone();
                clone.removeAttr("id");
                $(clone.children().get(0)).attr("id","names"+selectId+"filter");
                $(clone.children().get(1)).attr("inputIndex",selectId+"filter");
                $(clone.children().get(2)).attr("id","ids"+selectId+"filter");

                selectElement.parent().append(clone);
            }else if(selectValue=='BUS_UPDTIME'){//更新时间
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-3").clone();
                clone.removeAttr("id");
                selectElement.after($("#judgeCon-3").clone());
            }else if(selectValue=='BUS_STATUS'){//状态
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-1").clone();
                clone.removeAttr("id");
                selectElement.after($("#judgeCon-1").clone());
            }else{
                selectElement.nextAll().remove();
                var clone = $("#judgeCon-2").clone();
                clone.removeAttr("id");
                selectElement.after($("#judgeCon-2").clone());
            }
        }
    }
    //常量的改变 CommonVar
    function judgeConditionChange(elem) {

        var selectElement = $(elem);
        var selectId = selectElement.parent().prev().attr("id");
        var selectControlType = selectElement.parent().prev().find("option:selected").attr("ctltype");

        var selectValue = $(elem).find("option:selected").val();
        if(selectValue==3||selectValue==4){
            selectElement.parent().nextAll().remove();
            var clone = $("#judgeCon-6").clone();
            clone.removeAttr("id");
            selectElement.parent().parent().append(clone);
        }else{
            selectElement.parent().nextAll().remove();

            var clone;
            if(selectControlType==4||selectControlType==8){//人员
                clone = $("#judgeCon-7").clone();
            }else if(selectControlType==5||selectControlType==17){//角色
                clone = $("#judgeCon-9").clone();
            }else if(selectControlType==6||selectControlType==18){//组织
                clone = $("#judgeCon-8").clone();
            }else if(selectControlType==7||selectControlType==19){//岗位
                clone = $("#judgeCon-13").clone();
            }

            if(clone!=null){
                clone.removeAttr("id");
                $(clone.children().get(0)).attr("id","names"+selectId+"filter");
                $(clone.children().get(1)).attr("inputIndex",selectId+"filter");
                $(clone.children().get(2)).attr("id","ids"+selectId+"filter");
                selectElement.parent().parent().append(clone);
            }
        }
    }
    //脚本条件 选择按钮
    function scriptCondition(ele){
        var id = $(ele).next().attr("id");
        parent.layer.open({
            type: 2,
            title: "脚本窗口",
            shade: 0.3,
            area: ['750px', '420px'],
            content: serverPath.formPath + setter.formLinkPath +'formDef/view/filterScript?tableId='+tableId+'&add=0&id='+id
        })
    }

    //人员选择器
    function selectUser(obj) {
        var attrId = $(obj).attr("inputIndex");
        var userSelectorWin;
        var body;
        var userNameArray=[],userIdArray=[];
        parent.layer.open({
            type: 2,
            title: "人员选择器",
            shade: 0.3,
            resize: false,
            area: ['980px', '540px'],
            btn:["保存","取消","撤销"],
            content: serverPath.formPath + setter.sysLinkPath + "dialog/view/sysUserDialog",
            success: function(layero, index){
                body = parent.layer.getChildFrame('body',index);
                userSelectorWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                userSelectorWin.layui.userList=[];
                var names = $("#names"+attrId).val();
                var ids = $("#ids"+attrId).val();
                if(ids!="" && names!=""){
                    userNameArray = names.split(",");
                    userIdArray = ids.split(",");
                    for(var i=0;i<userIdArray.length;i++){
                        userSelectorWin.layui.userList.push({name:userNameArray[i],userId:userIdArray[i]});
                    }
                }
            },
            yes:function(index,layero){
                userNameArray=[],userIdArray=[];
                var theUserList = body.find("input[name=userList]").val();
                if(theUserList){
                    layui.each(JSON.parse(theUserList),function(index,item){
                        userNameArray.push(item.name);
                        userIdArray.push(item.userId);
                    });
                }
                $("#names"+attrId).val(userNameArray.join(","));
                $("#ids"+attrId).val(userIdArray.join(","));
                parent.layer.close(index);
            },
            btn3:function(index, layero){
                userSelectorWin.cancelData(index);
                return false //开启该代码可禁止点击该按钮关闭
            }
        });
    }
    //组织选择器
    function selectOrg(obj){
        var attrId = $(obj).attr("inputIndex");
        var orgSelectorWin;
        var body;
        var orgNameArray = [], orgIdArray = [];
        parent.layer.open({
            type: 2,
            title: "组织选择器",
            shade: 0.3,
            resize: false,
            area: ['980px', '540px'],
            btn:["保存","取消","撤销"],
            content: serverPath.formPath + setter.sysLinkPath + "dialog/view/sysOrgDialog",
            success: function (layero, index) {
                body = parent.layer.getChildFrame('body',index);
                orgSelectorWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                orgSelectorWin.layui.orgList = [];
                var names = $("#names" + attrId).val();
                var ids = $("#ids" + attrId).val();
                if (ids != "" && names != "") {
                    orgNameArray = names.split(",");
                    orgIdArray = ids.split(",");
                    for (var i = 0; i < orgIdArray.length; i++) {
                        orgSelectorWin.layui.orgList.push({name: orgNameArray[i], orgId: orgIdArray[i]});
                    }
                }
            },
            yes: function (index,layero) {
                orgNameArray = [], orgIdArray = [];
                var theOrgList = body.find("input[name=orgList]").val();
                if(theOrgList) {
                    layui.each(JSON.parse(theOrgList), function (index, item) {
                        orgNameArray.push(item.name);
                        orgIdArray.push(item.orgId);
                    });
                }
                $("#names" + attrId).val(orgNameArray.join(","));
                $("#ids" + attrId).val(orgIdArray.join(","));
                parent.layer.close(index);
            },
            btn3:function(index, layero){
                orgSelectorWin.cancelData(index);
                return false //开启该代码可禁止点击该按钮关闭
            }
        });
    }
    //角色选择器
    function selectRole(obj){
        var attrId = $(obj).attr("inputIndex");
        var roleSelectorWin;
        var body;
        var roleNameArray = [], roleIdArray = [];
        parent.layer.open({
            type: 2,
            title: "角色选择器",
            shade: 0.3,
            resize: false,
            area: ['980px', '540px'],
            btn:["保存","取消","撤销"],
            content: serverPath.formPath + setter.sysLinkPath + "dialog/view/sysRoleDialog",
            success: function (layero, index) {
                body = parent.layer.getChildFrame('body',index);
                roleSelectorWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                roleSelectorWin.layui.roleList = [];
                var names = $("#names" + attrId).val();
                var ids = $("#ids" + attrId).val();
                if (ids != "" && names != "") {
                    roleNameArray = names.split(",");
                    roleIdArray = ids.split(",");
                    for (var i = 0; i < roleIdArray.length; i++) {
                        roleSelectorWin.layui.roleList.push({name: roleNameArray[i], roleId: roleIdArray[i]});
                    }
                }
            },
            yes: function (index,layero) {
                roleNameArray = [], roleIdArray = [];
                var theRoleList = body.find("input[name=roleList]").val();
                if(theRoleList){
                    layui.each(JSON.parse(theRoleList), function (index, item) {
                        roleNameArray.push(item.name);
                        roleIdArray.push(item.roleId);
                    });
                }
                $("#names" + attrId).val(roleNameArray.join(","));
                $("#ids" + attrId).val(roleIdArray.join(","));
                parent.layer.close(index);
            },
            btn3:function(index, layero){
                roleSelectorWin.cancelData(index);
                return false //开启该代码可禁止点击该按钮关闭
            }
        });
    }
    //前台uuid
    function guid2() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }
    //在光标处插入数据
    $.fn.extend({
        insertAtCaret: function (myValue) {
            var $t = $(this)[0];
            if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            } else
            if ($t.selectionStart || $t.selectionStart == '0')
            {
                var startPos = $t.selectionStart;
                var endPos = $t.selectionEnd;
                var scrollTop = $t.scrollTop;
                $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos,$t.value.length);
                this.focus();
                $t.selectionStart = startPos + myValue.length;
                $t.selectionEnd = startPos + myValue.length;
                $t.scrollTop = scrollTop;
            } else {
                this.value += myValue;
                this.focus();
            }
        }
    });

</script>
<style>
    .layui-form-item{
        margin-bottom:0;
        border-bottom:1px solid #ccc;
    }
    .layui-form-labe{
        border-right: 1px solid #ccc;
        border-left: 1px solid #ccc;
        float: left;
        display: block;
        padding: 9px 15px;
        width: 60px;
        font-weight: 400;
        line-height: 20px;
        text-align: right;
    }
    .layui-form-label {
        border-right: 1px solid #ccc;
        float: left;
        display: block;
        padding: 9px 15px;
        width: 89px;
        font-weight: 400;
        line-height: 20px;
        text-align: right;
    }
    .layui-input-block{
        margin-left:130px;
    }
    .layui-elem-field{
        margin:0 15px;
        border:1px solid #ccc;
    }.layui-form-item .layui-inline{
        margin-bottom:0;
         }
    .table-detail td input{
        width: 150px;
        margin-left:0;
    }
    .table-detail td .layui-form-select{
        width: 150px;
        float: left;
    }
   #ruleDiv .layui-form-select{
        width: 120px;
       float:left;
    }
    #ruleDiv .inputBox{
        width: 100px;
        float: left;
    }
    #date,#date1{
        float: left;
        width: 120px;
    }
    .fieldOpt .layui-form-select{
        width: 182px;
    }
    .toolBar{
        margin: 0;
        width: 355px;
        position: absolute;
        right: 0;
    }
    .table-top-right{
        margin-bottom: 30px;
    }
    .table-detail th ,.layui-elem-field legend{
        font-size: 14px;
        font-weight: 400;
    }
    .help{
        position: absolute;
        top: 38px;
        width: 336px;
        z-index: 999;
        padding: 11px;
        background: #FFFFA3;
        left:30px;
        display: none;
    }
    .layui-form-select dl{
        max-height: 210px;
    }
    .scriptStyle .layui-form-checkbox[lay-skin=primary]{
        float: left;
        margin-left:5px;
    }
    .addScriptStyle .layui-form-checkbox[lay-skin=primary]{
        float: left;
        margin-left:5px;
    }
    #LAY_app{
        overflow: hidden;
    }
    .judge-condition .layui-form-checkbox[lay-skin=primary]{
        margin-top: 7px;
    }
    .judge-condition .layui-form-radio{
        margin-top:-4px;
    }
</style>
</body>
</html>